<template>
  <div>
    <el-row>
      <el-col>
        <el-table
          :data="salesList.slice((page-1)*pageSize,pageSize*page)"
          style="width: 100%"
          class="tableinfo margintop20"
          :header-cell-style="{'text-align':'center'}"
          :cell-style="{'text-align':'center'}"
          stripe
        >
          <el-table-column label="序号" type="index"></el-table-column>
          <el-table-column label="姓名" prop="username"></el-table-column>
          <el-table-column label="职位" prop="roleid">
            <template slot-scope="scope">
              <el-tag
                effect="plain"
                :type="scope.row.roleid | colortype"
              >{{scope.row.roleid | rolename}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="状态" prop="status">
            <template slot-scope="scope">
              <el-tag
                effect="plain"
                :type="scope.row.status | colortype"
              >{{scope.row.status | statusname}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="销售额(/元)" prop="sales"></el-table-column>
          <el-table-column label="提成" prop="sales1"></el-table-column>
          <el-table-column label="操作" width="150">
            <template slot-scope="scope">
              <el-button
                @click="editAdvise(scope.row)"
                v-if="roleid*1==4"
                size="mini"
                type="success"
              >编辑</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>

    <el-dialog title="修改用户" :visible.sync="dialogVisible" width="40%" :before-close="handleClose">
      <el-form
        :model="form"
        :rules="rules"
        ref="myform"
        label-width="100px"
        label-position="right"
        class="myform"
      >
        <el-form-item label="姓名" prop="username">
          <el-input v-model="form.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="职位" prop="roleid">
          <el-select v-model="form.roleid" disabled>
            <el-option
              v-for="(role,i) in roleList"
              :key="i"
              :label="role.label"
              :value="role.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="销售额(/元)" prop="sales">
          <el-input v-model="form.sales"></el-input>
        </el-form-item>
        <el-form-item label="提成" prop="sales1">
          <el-input v-model="form.sales1"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateById()">确 定</el-button>
      </span>
    </el-dialog>

    <div class="flexbox">
      <el-pagination
        background
        layout="prev, pager, next"
        @current-change="changeCurrent"
        :page-size="pageSize"
        :current-page="page"
        :total="total"
      ></el-pagination>
      <span>共 {{total}} 条数据</span>
    </div>
  </div>
</template>


<script>
import { getuserlist, updateusername } from "@/api";
import { roleList, statusList } from "@/utils";
import Vue from "vue"
var roleid = sessionStorage.getItem("roleid");
export default {
  data() {
    return {
      salesList: [],
      page: 1,
      pageSize: 6,
      roleid,
      dialogVisible: false,
      form: {},
      rules: {
        sales: [
          { required: true, message: "请输入销售额", trigger: "change" },
          {
            validator: function (rule, value, callback) {
              var passReg = /^[0-9]*$/;
              if (!passReg.test(value)) {
                callback(new Error("请输入正确格式的金额"));
              } else {
                callback();
              }
            },
            trigger: "change",
          },
        ],
        sales1: [
          { required: true, message: "请输入提成金额", trigger: "blur" },
          {
            validator: function (rule, value, callback) {
              var passReg = /^[0-9]*$/;
              if (!passReg.test(value)) {
                callback(new Error("请输入正确格式的金额"));
              } else {
                callback();
              }
            },
            trigger: "change",
          },
        ],
      },
      roleList,
    };
  },
  computed: {
    total() {
      return this.salesList.length;
    },
  },
  methods: {
    handleClose() {
      this.dialogVisible = false;
    },
    changeCurrent(page) {
      this.page = page;
    },
    editAdvise(row) {
      if (row.status * 1 == 2) {
        this.$message.error("此用户已被停用..");
      } else {
      (this.dialogVisible = true), (this.form = { ...this.form, ...row });
      }
    },
    delUser() {},
    updateById() {
      this.$refs.myform.validate((valid) => {
        if (valid) {
          updateusername(this.form)
          .then((res) => {
            if (res.type) {
              console.log(res.result);
              this.dialogVisible = false;
              this.salesList.forEach((item, i) => {
                if (item._id == this.form._id) {
                  Vue.set(this.salesList, i, this.form);
                }
              });
            }
          });
        }
      });
    },
  },
  mounted() {
    getuserlist().then((res) => {
      if (res.type) {
        this.salesList = res.result;
      }
    });
  },
};
</script>